﻿/*
 * Copyright 2011 Hanns Kucer
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
using System;
using System.Collections.Generic;
using System.Text;

namespace iTachToolkit
{
    public class Transmission
    {
        public class Command
        {
            public class APIFunction
            {
                public enum Mnemonic
                {
                    unknown = -1,
                    nop = 0,
                    sendir = 1,
                    getversion = 2,
                    completeir = 3,
					stopir,
                }

                public readonly IR Ir;
                public readonly Mnemonic Mnemo;

                internal APIFunction(string[] parts)
                {
                    Mnemo = Mnemonic.nop;

                    if (parts == null || parts.Length == 0)
                        return;

                    switch (parts[0].Trim().ToLower())
                    {
                        case "sendir":
                            Mnemo = Mnemonic.sendir;
                            Ir = new IR(parts);
                            break;
                        case "getversion":
                            Mnemo = Mnemonic.getversion;
                            break;
                        case "completeir":
                            Mnemo = Mnemonic.completeir;
							break;
						case "stopir":
                            Mnemo = Mnemonic.stopir;
                            break;
                        default:
                            Mnemo = Mnemonic.unknown;
                            break;
                    }
                }
            }
            public readonly DateTime Timestamp;

            private APIFunction _function;
            public APIFunction Function
            {
                get
                {
					if (!_parsed) ParseModular();
					return _function;
                }
                internal set
                {
                    _function = value;
                }
            }

            public readonly string Data;

            private int _module = -1;
            public int Module
            {
                get
                {
                    if (!_parsed) ParseModular();
                    return _module;
                }
            }

            private int _connector = -1;
            public int Connector
            {
                get
                {
                    if (!_parsed) ParseModular();
                    return _connector;
                }
            }

            private bool _parsed = false;
            private void ParseModular()
            {
                lock (this)
                {
                    if (_parsed) return;
                    string[] p = Data.Split(',');
                    Function = new APIFunction(p);
                    if (p.Length >= 2)
                    {
                        p = p[1].Split(':');
                        if (p.Length != 2 || !int.TryParse(p[0], out _module) || !int.TryParse(p[1], out _connector))
                        {
                            _module = -1;
                            _connector = -1;
                        }
                    }
                    _parsed = true;
                }
            }

            internal Command(string data)
            {
                Timestamp = DateTime.Now;
                Data = data;
            }
        }

        public readonly Command Request;
        private Command _response;
        public Command Response
        {
            get
            {
                return _response;
            }
            internal set
            {
                _response = value;
            }
        }

        public Transmission(string data)
        {
            Request = new Command(data);
        }

        public Transmission(string requestData, string responseData)
        {
            Request = new Command(requestData);
            Response = new Command(responseData);
        }
    }
}
